package com.project.lk.nurseapp.nurse;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.base.BaseActivity;
import com.project.lk.nurseapp.base.UrlContants;
import com.project.lk.nurseapp.doctor.DoctorDetailsActivity;
import com.project.lk.nurseapp.nurse.model.ChatListItemBean;
import com.project.lk.nurseapp.nurse.presenter.ChatListPresenter;
import com.project.lk.nurseapp.nurse.presenter.IChatListView;
import com.project.lk.nurseapp.utils.AppManager;
import com.project.lk.nurseapp.utils.LocationList;
import com.project.lk.nurseapp.utils.PopWindowManager;
import com.project.lk.nurseapp.utils.XViewSettingUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.http.Url;
/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class ChatListActivity extends BaseActivity implements IChatListView {


    @BindView(R.id.xrecyclerview)
    XRecyclerView xrecyclerview;
    private ChatListAdapter chatAdapter;
    private PopupWindow commentPopupWindow;
    private View commentContentView;
    private View praentView;
    private List<CommentStartBean> startList;
    private ChatListPresenter presenter;
    private EditText commentEdit;
    private List<ChatListItemBean> chatList;
    private String sessionId;

    /*0 是查询正在进行的会话  1是查询已结束的会话  -1是查询所有会话
    *
    */

    String sessionStatus="-1";
    private int chosePosition;

    @Override
    protected void initData() {

        presenter = new ChatListPresenter(this);
        chatAdapter = new ChatListAdapter(presenter.getList(),this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        XViewSettingUtils.setting(xrecyclerview, chatAdapter,manager);
        setCommentPopwind();
        presenter.refresh(UrlContants.SESSION_LIST_URL,sessionStatus);
        
    }
    int starPosition;
    //设置弹窗
    private void setCommentPopwind() {
        praentView = LayoutInflater.from(this).inflate(R.layout.activity_msg_list, null);
        commentContentView = LayoutInflater.from(this).inflate(R.layout.comment_popwind_layout, null);
        commentPopupWindow = new PopupWindow(commentContentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);

        ImageView cancelBut = (ImageView) commentContentView.findViewById(R.id.cancel_but);
        commentEdit = (EditText) commentContentView.findViewById(R.id.comment_edit);
        Button  commentBut = (Button) commentContentView.findViewById(R.id.comment_but);
        RecyclerView commentRecyclerView = commentContentView.findViewById(R.id.comment_rv);
        startList = new ArrayList<>();
        for (int i=0;i<5;i++){
            CommentStartBean bean=new CommentStartBean();
            startList.add(bean);
        }

        final CommentStartAdapter adapter= new CommentStartAdapter(startList,this);
        StaggeredGridLayoutManager manager=new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL);
        commentRecyclerView.setAdapter(adapter);
        commentRecyclerView.setLayoutManager(manager);

        adapter.setItemEvent(new CommentStartAdapter.ItemEventListenter() {
            @Override
            public void start(int position) {
                starPosition=position;
                for (int i=0;i<startList.size();i++){
                    if (i<=position){
                        startList.get(i).setStart(true);
                    }else {
                        startList.get(i).setStart(false);
                    }
                }
                adapter.setmList(startList);
                adapter.notifyDataSetChanged();
            }
        });

        //点击事件
        setEvent(cancelBut, commentBut, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.cancel_but: //取消
                        commentPopupWindow.dismiss();
                        break;
                    case R.id.comment_but:  //评价
                        Log.e("MLOG",sessionId+">>>>>>>>>>");
                        if (!TextUtils.isEmpty(commentEdit.getText())) {
                            presenter.evaluateChat(UrlContants.EVALUATE_SESSION_URL,sessionId,String.valueOf(starPosition),commentEdit.getText().toString());
                        }else {
                            presenter.evaluateChat(UrlContants.EVALUATE_SESSION_URL,sessionId,String.valueOf(starPosition),"");
                        }
                        commentPopupWindow.dismiss();
                        break;
                }

            }
        });
    }
    //设置点击事件
    private void setEvent(ImageView cancelBut, Button commentBut, View.OnClickListener listener) {
        cancelBut.setOnClickListener(listener);
        commentBut.setOnClickListener(listener);
    }
    @Override
    protected void initView() {
        chatAdapter.setItemEvent(new ChatListAdapter.ItemEventListenter() {
            @Override
            public void comment(int position) { //弹窗：
                sessionId = chatList.get(position).getId();
                chosePosition = position;
                PopWindowManager.showAsLocationWindow(praentView,commentContentView
                        ,commentPopupWindow,R.id.comment_popwind_layout);
            }

            @Override
            public void look(int position) {  //查看医嘱
                String id = chatList.get(position).getId();
                startActivity(new Intent(ChatListActivity.this, DoctorDetailsActivity.class)
                .putExtra("sessionId",id));
            }
        });

        xrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                presenter.refresh(UrlContants.SESSION_LIST_URL,sessionStatus);
            }

            @Override
            public void onLoadMore() {
                presenter.loadMore(UrlContants.SESSION_LIST_URL,sessionStatus);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_msg_list;
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        AppManager.getAppManager().finishActivity();
    }

    @Override
    public void showList() {
        if (chatAdapter!=null) {
            chatList = presenter.getList();
            chatAdapter.notifyDataSetChanged();
            xrecyclerview.loadMoreComplete();
            xrecyclerview.refreshComplete();
        }
    }

    @Override
    public void noMore() {
        if (xrecyclerview!=null) {
            xrecyclerview.loadMoreComplete();
            xrecyclerview.refreshComplete();
        }

    }

    @Override
    public void fail() {
        if (xrecyclerview!=null) {
            xrecyclerview.loadMoreComplete();
            xrecyclerview.refreshComplete();
        }
    }

    @Override
    public void evaluateSuccess() {
        if (chatAdapter!=null) {
            List<ChatListItemBean> list = chatAdapter.getList();
            list.get(chosePosition).setEvaluateStar("3");
            chatAdapter.setList(list);
            chatAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void evaluateFail() {

    }
}
